package com.xxxy.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxxy.commonResult.CommonResult;
import com.xxxy.log.Log;
import com.xxxy.model.ExamplePaper;
import com.xxxy.model.TeacherReviewed;
import com.xxxy.response.*;
import com.xxxy.service.ExamplePaperService;
import com.xxxy.service.ReviewedService;
import com.xxxy.service.TeacherReviewedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author StevenShen
 * @date 2022/3/8 10:20
 */
@RestController
@Api(tags = "批阅管理")
@RequestMapping("/reviewed")
public class ReviewedController {

    @Autowired
    private ReviewedService reviewedService;

    @Autowired
    private ExamplePaperService examplePaperService;

    @Autowired
    private TeacherReviewedService teacherReviewedService;

    @Log(module = "批阅管理", description = "开始阅卷")
    @PostMapping("/start")
    @ApiOperation("开始阅卷")
    public CommonResult<Boolean> startReviewed(@ApiParam("试卷id") @RequestParam Integer examinationPaperId) {
        return CommonResult.success(reviewedService.startReviewed(examinationPaperId));
    }

    @Log(module = "批阅管理", description = "管理员获取样卷")
    @GetMapping("/get/example")
    @ApiOperation("管理员获取样卷")
    public CommonResult<ExamplePaperResponse> getExamplePaper(@ApiParam("试卷id") @RequestParam Integer examinationPaperId) {
        ExamplePaperResponse examplePaper = examplePaperService.getExamplePaper(examinationPaperId);
        if (examplePaper == null) {
            return CommonResult.success("试卷不存在");
        }
        return CommonResult.success(examplePaper);
    }

    @Log(module = "批阅管理", description = "管理员提交样卷")
    @PostMapping("/submit/example")
    @ApiOperation("管理员提交样卷")
    public CommonResult<Boolean> submitExamplePaper(
            @ApiParam("学生答案编号") @RequestParam Integer reviewedId,
            @ApiParam("该题在试卷中的位置列表") @RequestParam String questionIndex,
            @ApiParam("分数列表") @RequestParam String scores) {
        return CommonResult.success(examplePaperService.submit(reviewedId, questionIndex, scores));
    }

    @Log(module = "批阅管理", description = "管理员设置允许误差")
    @PostMapping("/set/error")
    @ApiOperation("管理员设置允许误差")
    public CommonResult<Boolean> setAllowableError(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("允许误差") @RequestParam Integer allowableError) {
        ExamplePaper examplePaper = new ExamplePaper();
        examplePaper.setPaperId(examinationPaperId);
        examplePaper.setAllowableError(allowableError);
        return CommonResult.success(examplePaperService.save(examplePaper));
    }

    @Log(module = "批阅管理", description = "管理员获取批阅试题列表")
    @GetMapping("/get/question")
    @ApiOperation("管理员获取批阅试题列表")
    public CommonResult<List<ReviewedQuestionListResponse>> getReviewedQuestion(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId) {
        return CommonResult.success(reviewedService.getQuestionList(examinationPaperId));
    }

    @Log(module = "批阅管理", description = "管理员设置阅卷老师")
    @PostMapping("/set/teacher")
    @ApiOperation("管理员设置阅卷老师")
    public CommonResult<Boolean> setReviewedTeacher(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("题号") @RequestParam Integer questionIndex,
            @ApiParam("教师账号") @RequestParam String teacherAccount) {
        TeacherReviewed teacherReviewed = new TeacherReviewed();
        teacherReviewed.setPaperId(examinationPaperId);
        teacherReviewed.setQuestionIndex(questionIndex);
        teacherReviewed.setTeacherAccount(teacherAccount);
        return CommonResult.success(teacherReviewedService.save(teacherReviewed));
    }

    @Log(module = "批阅管理", description = "教师获取批阅试题列表")
    @GetMapping("teacher/get/question")
    @ApiOperation("教师获取批阅试题列表")
    public CommonResult<List<ReviewedQuestionListResponse>> teacherGetReviewedQuestion(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("教师id") @RequestParam String teacherAccount) {
        return CommonResult.success(teacherReviewedService.getQuestionList(examinationPaperId, teacherAccount));
    }

    @Log(module = "批阅管理", description = "教师获取待批阅考卷")
    @GetMapping("/get/paper")
    @ApiOperation("教师获取待批阅考卷")
    public CommonResult<List<ReviewedPaperListResponse>> getReviewedPaper(
            @ApiParam("教师id") @RequestParam String teacherAccount) {
        return CommonResult.success(teacherReviewedService.getReviewedList(teacherAccount));
    }

    @Log(module = "批阅管理", description = "教师批阅样卷")
    @PostMapping("/review/example")
    @ApiOperation("教师批阅样卷")
    public CommonResult<ExamplePaperTeacherResponse> reviewExamplePaper(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("题号") @RequestParam Integer questionIndex) {
        return CommonResult.success(teacherReviewedService.getExampleQuestion(examinationPaperId, questionIndex));
    }

    @Log(module = "批阅管理", description = "设置教师通过样卷测试")
    @PostMapping("/set/passExample")
    @ApiOperation("设置教师通过样卷测试")
    public CommonResult<Boolean> setPassExample(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("题号") @RequestParam Integer questionIndex,
            @ApiParam("教师id") @RequestParam String teacherAccount) {
        UpdateWrapper<TeacherReviewed> wrapper = new UpdateWrapper<>();
        wrapper.set("pass_example", 1);
        wrapper.eq("paper_id", examinationPaperId);
        wrapper.eq("question_index", questionIndex);
        wrapper.eq("teacher_account", teacherAccount);
        return CommonResult.success(teacherReviewedService.update(wrapper));
    }

    @Log(module = "批阅管理", description = "教师批阅试卷")
    @PostMapping("/review")
    @ApiOperation("教师批阅试卷")
    public CommonResult<ReviewedQuestionResponse> reviewPaper(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("题号") @RequestParam Integer questionIndex) {
        ReviewedQuestionResponse reviewedQuestion = teacherReviewedService.getReviewedQuestion(examinationPaperId, questionIndex);
        if (reviewedQuestion == null) {
            return CommonResult.success("已批阅完成");
        }
        return CommonResult.success(reviewedQuestion);
    }

    @Log(module = "批阅管理", description = "教师打分")
    @PostMapping("/review/score")
    @ApiOperation("教师打分")
    public CommonResult<Boolean> setScore(
            @ApiParam("学生答案编号") @RequestParam Integer reviewedId,
            @ApiParam("题号") @RequestParam Integer questionIndex,
            @ApiParam("分数") @RequestParam Double score,
            @ApiParam("教师id") @RequestParam String teacherAccount) {
        return CommonResult.success(teacherReviewedService.setScore(reviewedId, questionIndex, score, teacherAccount));
    }

    @Log(module = "批阅管理", description = "管理员获取题目的进度")
    @GetMapping("/get/question/progress")
    @ApiOperation("管理员获取题目的进度")
    public CommonResult<List<ReviewedQuestionProgressResponse>> getQuestionProgress(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId) {
        return CommonResult.success(reviewedService.getQuestionProgress(examinationPaperId));
    }

    @Log(module = "批阅管理", description = "管理员获取试卷的进度")
    @GetMapping("/get/paper/progress")
    @ApiOperation("管理员获取试卷的进度")
    public CommonResult<ReviewedPaperProgressResponse> getReviewedProgress(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId) {
        return CommonResult.success(reviewedService.getReviewedProgress(examinationPaperId));
    }

    @Log(module = "批阅管理", description = "获取教师的批阅进度")
    @GetMapping("/get/teacher/progress")
    @ApiOperation("获取教师的批阅进度")
    public CommonResult<List<TeacherProgressResponse>> getTeacherProgress(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("题号") @RequestParam Integer questionIndex) {
        return CommonResult.success(teacherReviewedService.getTeacherProgress(examinationPaperId, questionIndex));
    }

    @Log(module = "批阅管理", description = "教师获取试卷的进度")
    @GetMapping("/teacher/get/paper/progress")
    @ApiOperation("教师获取试卷的进度")
    public CommonResult<TeacherPaperProgressResponse> teacherGetPaperProgress(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("教师id") @RequestParam String teacherAccount) {
        return CommonResult.success(teacherReviewedService.getPaperProgress(examinationPaperId, teacherAccount));
    }

    @Log(module = "批阅管理", description = "教师获取试题的进度")
    @GetMapping("/teacher/get/question/progress")
    @ApiOperation("教师获取试题的进度")
    public CommonResult<ReviewedPaperProgressResponse> teacherGetQuestionProgress(
            @ApiParam("试卷id") @RequestParam Integer examinationPaperId,
            @ApiParam("教师id") @RequestParam String teacherAccount) {
        return CommonResult.success(teacherReviewedService.getQuestionProgress(examinationPaperId, teacherAccount));
    }

}
